#ifndef MICROFORSOURCE_H
#define MICROFORSOURCE_H

#include "datasource.h"
#include <QDateTime>


#pragma pack(push, 1)
struct TData
{
    double date; //8 bytes
    float humidity; //4 bytes;
    float temperature; //4 bytes
    float addition; //4bytes
public:
    TData():date(0){}

    QDateTime getDate()
    {
        QDate  beginDate = QDate(1899,12,30);
        beginDate = beginDate.addDays((int)date);

        double ceil = int (date);
        double drob = date - ceil;

        double time = 3600.0*24.0*drob;

        unsigned int hours = (int)time / 3600;
        unsigned int minutes = ((int)time - hours*3600) / 60;
        unsigned int seconds = ((int) time - hours*3600 - minutes * 60);

        QTime tm(hours,minutes,seconds,0);

        QDateTime dat;
        dat.setDate(beginDate);
        dat.setTime(tm);

        return dat;
    }
    bool isValid()
    {
        if (date == 0) return false;

        QDateTime limit;
        limit.setDate(QDate(2100,1,1));
        if (getDate() > limit) return false;

        limit.setDate(QDate(1900,1,1));
        if (getDate() < limit) return false;

        return true;
    }
};
#pragma pack(pop)

class CMicroforSource : public FDataSource
{
protected:
    TData readData();
public:
    CMicroforSource(QVariant param):FDataSource(param){}
};

class CMicroforHumiditySource: public CMicroforSource
{
public:
    CMicroforHumiditySource(QVariant param):CMicroforSource(param){}
    virtual CValue readValue(){
        TData d = readData();
        if (!d.isValid()) return CValue();
        return CValue(d.humidity);
    }
};

class CMicroforTemperatureSource: public CMicroforSource
{
public:
    CMicroforTemperatureSource(QVariant param):CMicroforSource(param){}
    virtual CValue readValue(){
        TData d = readData();
        if (!d.isValid()) return CValue();
        return CValue(d.temperature);
    }
};

#endif // MICROFORSOURCE_H
